package controllers;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import play.mvc.*;
 
import javax.persistence.EntityManager;


import models.Medico;
import models.Paciente;
import play.db.jpa.JPA;

public class Application extends Controller {

	    
	    public void cadastro(String nome,String prontuario, String diagnostico, int telefone, char sexo,
				String procedencia, String endereco, boolean historicoFamiliar, int dia, int mes, int ano) {  
	    	
	    	String historico;
	    	if (historicoFamiliar)
	    		historico = "true";
	    	else
	    		historico = "false";
	    	
	        try {  
	            Connection con;  
	  
	               
	  
	            Class.forName("org.postgresql.Driver");  
	            con = (Connection) DriverManager.getConnection(  
	                    "jdbc:postgresql://localhost:5432/sipro",  
	                    "postgres",  
	                    "mmunaretto");  
	            Statement stm = (Statement) con.createStatement();  
	             
	            if (prontuario.equalsIgnoreCase("")) {  
	                String sql = "INSERT INTO paciente(nomePac,dataNASC,endPac,telPac,localPac,historicoPac,sexo,prontPac) VALUES('" 
	                			+ nome + "','" + ano + "-" + mes + "-" + dia + "','" + endereco + "'," + telefone + ",'" + procedencia +
	                			"','" + historico + "','" + sexo + "','" + prontuario + "')";
	                			
	                stm.executeUpdate(sql);  
	                con.close();  
	            } else {  
	                String sql = "UPDATE paciente(nomePac,dataNASC,endPac,telPac,localPac,historicoPac,sexo,prontPac) VALUES('" 
	                			+ nome + "','" + ano + "-" + mes + "-" + dia + "','" + endereco + "'," + telefone + ",'" + procedencia +
	                			"','" + historico + "','" + sexo + "','" + prontuario + "')";
	                stm.executeUpdate(sql);  
	                con.close();  
	            }  
	        } catch (ClassNotFoundException ex) {  
	            Logger.getLogger(Paciente.class.getName()).log(Level.SEVERE, null, ex);  
	        } catch (SQLException ex) {  
	            Logger.getLogger(Paciente.class.getName()).log(Level.SEVERE, null, ex);  
	        }  
	  
	    }  
	    
}